package com.ioc.business.order.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.ioc.business.order.domain.DeliveryOrder;
import com.ioc.business.order.domain.bo.delivery.data.board.DeliveryDataBoardQueryBo;
import com.ioc.business.order.mapper.DeliveryOrderMapper;
import com.ioc.business.order.service.IDeliveryDataBoardService;
import com.ioc.business.order.vo.delivery.DeliveryOrderPageVo;
import com.ioc.business.order.vo.delivery.data.board.DeliveryDataBoardPageVo;
import com.ioc.common.core.page.TableVo;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class DeliveryDataBoardServiceImpl implements IDeliveryDataBoardService {

    private final DeliveryOrderMapper deliveryOrderMapper;

    @Override
    public DeliveryDataBoardPageVo selectDeliveryDataBoardPageList(DeliveryDataBoardQueryBo queryBo) {
        List<DeliveryOrder> result = deliveryOrderMapper
                .queryDeliveryDataBoardPageList(queryBo);
        List<DeliveryOrderPageVo> pageVos = new ArrayList<>(result.size());
        if (CollectionUtil.isNotEmpty(result)) {
            pageVos = buildPageVos(result);
        }
        List<DeliveryOrder> list = deliveryOrderMapper
                .queryDeliveryDataBoardList(queryBo);
        return new DeliveryDataBoardPageVo(new TableVo<>(result, pageVos), list);
    }

    private List<DeliveryOrderPageVo> buildPageVos(List<DeliveryOrder> records) {
        return records.stream().map(record -> {
            DeliveryOrderPageVo vo = new DeliveryOrderPageVo();
            BeanUtils.copyProperties(record, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}
